// 主界面
;define(function(require, exports, module) {
    'use strict';
    var util = require("util");
    var base = require("base");
    var leftMenu = require("../leftMenu/leftMenu");
    var topMenu = require("../topMenu/topMenu");
    var router = require("../hash/pageRouter");
    var dialog = require("dialog");
    var validate = require("validate");
    var css_detail_info = require("./financingDetailInfo.css");
    var constant = require("constant")
    var pageIndex = 1,
        pageSize = 10,
        totalPageCount = 1,
        totalRecordCount = 0; //分页初始化
    var financeId = "";
    var init = function(num){
    	leftMenu.init(num);
    	topMenu.init(1);
    	$(".footer").removeClass("none");
    	$(".table-section").removeClass("none");
    	$("#apply").addClass("leftMenu-active");
    	$("#leftMenu6 .leftMenu li:first-child").addClass("leftMenu-open");
    	inquireFinanceList();
		initEvent();
		router.init();
    };
	var initEvent = function(){
		//若未登录，则返回登陆页面
		if(base.getData("sid")==null||base.getData("sid")==""||base.getData("sid")==undefined){
			top.location.href="../login/login.html";
		}
		
		$("#main").on("click", "#detail", function(){
			var financeId = [];
           var $this = $(this).parents("tr.tr_style");
		   financeId.push($this.attr("financeId"));
			router.route("financing/financingDetailInfo.html?financeId="+financeId,true);
		});
		
		$("#main").on("click", "#financeSearch", function(){
			inquireFinanceList();
		});
    };
    
    //改变融资状态背景色
    var changeStatusColor = function(){
    	var status = $(".order_status");
    	for(var i=0,j=status.length;i<j;i++){
    		switch($.trim(status[i].innerHTML)){
    		case "融资":
    			$(status[i]).css("background-color", "#6BA8E7");
    			break;
    		case "换货":
    			$(status[i]).css("background-color", "#28BF85");
    			break;
    		}
    	}
    };
    
    //查询融资信息列表
    var inquireFinanceList = function(){
        base.fnAjax({
            action: "inquireFinanceList.json",
            async: false, //同步异步
			nocode: false, //是否要登陆，true为不用登陆
            oData: {
				pageIndex: pageIndex,
				pageSize: pageSize
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                	juicer.register("getValueByKey", constant.getValueByKey);
                    var tpl = $("#financeListTpl").html();
                    var html = juicer(tpl, data);
                    $("#financeListBody").html(html);
                    
                    
                    totalPageCount = data.totalPageCount;
                    totalRecordCount = data.totalRecordCount;
                    base.pageControl(
                    	$("#pagenation"),
                        totalRecordCount,
                        data.pageIndex,
                        data.pageSize,
                        function(p) {
                            pageIndex = p;
                            inquireFinanceList();
                    });
                    changeStatusColor();
                } else {
                    base.alert(2,"信息获取失败");
                }
            }
        });
    };
	
    return {
        init:init
    };
});